package com.ruoyi.web.controller.eamp;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.eamp.domain.EampDutyList;
import com.ruoyi.eamp.domain.vo.DutyVo;
import com.ruoyi.eamp.service.IEampDutyListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 值班表Controller
 *
 * @author ruoyi
 * @date 2024-08-08
 */
@RestController
@RequestMapping("/eamp/duty_list")
public class EampDutyListController extends BaseController
{
    @Autowired
    private IEampDutyListService eampDutyListService;

    /**
     * 查询值班表列表
     */
    @PreAuthorize("@ss.hasPermi('eamp:duty_list:list')")
    @GetMapping("/list")
    public TableDataInfo list(EampDutyList eampDutyList)
    {
        startPage();
        List<EampDutyList> list = eampDutyListService.selectEampDutyListList(eampDutyList);
        return getDataTable(list);
    }

    /**
     * 导出值班表列表
     */
    @PreAuthorize("@ss.hasPermi('eamp:duty_list:export')")
    @Log(title = "值班表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EampDutyList eampDutyList)
    {
        List<EampDutyList> list = eampDutyListService.selectEampDutyListList(eampDutyList);
        ExcelUtil<EampDutyList> util = new ExcelUtil<EampDutyList>(EampDutyList.class);
        util.exportExcel(response, list, "值班表数据");
    }

    /**
     * 获取值班表详细信息
     */
    @PreAuthorize("@ss.hasPermi('eamp:duty_list:query')")
    @GetMapping(value = "/{dutyId}")
    public AjaxResult getInfo(@PathVariable("dutyId") Long dutyId)
    {
        return success(eampDutyListService.selectEampDutyListByDutyId(dutyId));
    }

    /**
     * 新增值班表
     */
    @PreAuthorize("@ss.hasPermi('eamp:duty_list:add')")
    @Log(title = "值班表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EampDutyList eampDutyList)
    {
        eampDutyList.setCreateTime(new Date());
        System.out.println(eampDutyListService.insertEampDutyList(eampDutyList));
        return toAjax(1);

    }

    /**
     * 修改值班表
     */
    @PreAuthorize("@ss.hasPermi('eamp:duty_list:edit')")
    @Log(title = "值班表", businessType = BusinessType.UPDATE)
    @PutMapping("/add")
    public AjaxResult edit(@RequestBody EampDutyList eampDutyList)
    {
        return toAjax(eampDutyListService.updateEampDutyList(eampDutyList));
    }

    /**
     * 删除值班表
     */
    @PreAuthorize("@ss.hasPermi('eamp:duty_list:remove')")
    @Log(title = "值班表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dutyIds}")
    public AjaxResult remove(@PathVariable Long[] dutyIds)
    {
        return toAjax(eampDutyListService.deleteEampDutyListByDutyIds(dutyIds));
    }

    /**
     * 查询值班表列表
     */
    @PreAuthorize("@ss.hasPermi('eamp:duty_list:list')")
    @GetMapping("/getDutylist")
    public TableDataInfo getDutylist(DutyVo dutyVo)
    {
        startPage();
        List<EampDutyList> dutList = eampDutyListService.getListDuty(dutyVo);
        long total = new PageInfo(dutList).getTotal();


        List<Long> dutyIdList = new ArrayList<>();

        for (EampDutyList eve:dutList) {
            Long dutyId = eve.getDutyId();
            dutyIdList.add(dutyId);
        }
        // // 获取排班的id-根据排班id来
        List<DutyVo> list = eampDutyListService.getDutylist(dutyIdList);
        TableDataInfo rspDate = new TableDataInfo();
        rspDate.setCode(HttpStatus.SUCCESS);
        rspDate.setMsg("查询成功");
        rspDate.setRows(list);
        rspDate.setTotal(total);
        return rspDate;
    }




}
